//
//  OwnerInfoCalloutView.m
//  PhongThuyHuongNha
//
//  Created by Le Anh Tai on 10/19/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "OwnerInfoCalloutView.h"
#import "UIConstants.h"

@implementation OwnerInfoCalloutView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        //setup callout
        self.arrowPos = ArrowPos_TopCenter;
        self.alphaColor = COLOR_WHITE;
        self.borderColor = COLOR_WHITE;
        
        _subView = [[UIView alloc] init];
        _subView.backgroundColor = [UIColor clearColor];
        [self addSubview:_subView];
        
        //birthday label
        _birthdayLabel = [[UILabel alloc] init];
        _birthdayLabel.textAlignment = NSTextAlignmentCenter;
        _birthdayLabel.font = FONT_HELVETICANEUE(15);
        _birthdayLabel.textColor = COLOR_BLACK_PURE;
        _birthdayLabel.text = @"Ngày sinh";
        _birthdayLabel.backgroundColor = [UIColor clearColor];
        [_subView addSubview:_birthdayLabel];
        
        //date picker
        _birthdayPicker = [[FlatDatePicker alloc] init];
        _birthdayPicker.isShowHeader = NO;
        _birthdayPicker.delegate = self;
        [_subView addSubview:_birthdayPicker];
        
        //gender label
        _genderLabel = [[UILabel alloc] init];
        _genderLabel.textAlignment = NSTextAlignmentCenter;
        _genderLabel.font = FONT_HELVETICANEUE(15);
        _genderLabel.textColor = COLOR_BLACK_PURE;
        _genderLabel.text = @"Giới tính";
        _genderLabel.backgroundColor = [UIColor clearColor];
        [_subView addSubview:_genderLabel];
        
        //gender switch
        _genderSwitch = [[SevenSwitch alloc] init];
        _genderSwitch.thumbTintColor = [UIColor colorWithRed:0.19f green:0.23f blue:0.33f alpha:1.00f];
        _genderSwitch.activeColor = [UIColor grayColor];
        _genderSwitch.inactiveColor = COLOR_PINK;
        _genderSwitch.onTintColor = COLOR_BLUE_IOS7;//
        _genderSwitch.borderColor = [UIColor clearColor];
        _genderSwitch.shadowColor = [UIColor blackColor];        
        _genderSwitch.activeText = @"Nam";
        _genderSwitch.inactiveText = @"Nữ";
        _genderSwitch.activeTextColor = [UIColor whiteColor];
        _genderSwitch.inactiveTextColor = [UIColor whiteColor];
        _genderSwitch.on = DEFAULT_GENGER_MALE;
        [_genderSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
        [_subView addSubview:_genderSwitch];
    }
    
    return self;
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    
    _subView.frame = RECT(self.padding.left*2,
                          self.padding.top,
                          self.width - self.padding.right*2 - self.padding.left*2,
                          0);
    
    _birthdayLabel.frame = RECT(0, 0, _subView.width, 35);
    _birthdayPicker.frame = RECT(0, _birthdayLabel.bottom, _subView.width, 0);
    _genderLabel.frame = RECT(0, _birthdayPicker.bottom, _subView.width/2, 35);
    _genderSwitch.frame = RECT(_genderLabel.right, _genderLabel.top + 4, _subView.width - _genderLabel.right, _genderLabel.height - 8);
    
    _subView.height = _genderLabel.bottom + OWNER_INFO_HEIGHT_BUTTON;
    
    _calculatedHeight = _subView.bottom + self.padding.top + self.padding.bottom;

}

- (UIView *)subView
{
    return _subView;
}

- (void)close
{
    if (self.superview == nil)
    {
        return;
    }
    
//    [UIView transitionWithView:self
//                      duration:1
//                       options:UIViewAnimationOptionTransitionCurlUp
//                    animations:^ {
//                        self.height = 0;
//                    }
//                    completion:^(BOOL finished) {
//                        if (finished)
//                        {
//                            [self removeFromSuperview];
//                        }
//                        
//                    }];
//    
    [self removeFromSuperview];
    
    if ([self.delegate respondsToSelector:@selector(OwnerInfoCalloutViewDidClosed:)])
    {
        [self.delegate OwnerInfoCalloutViewDidClosed:self];
    }
}

- (void)showInView:(UIView *)view
{
    if (view == nil || self.superview)
    {
        return;
    }
    
    [view addSubview:self];
    
    [self loadData];
}

- (void)loadData
{
    [_birthdayPicker setDate:_birthday animated:YES];
    [_birthdayPicker setIsAmLich:_isAmLich];    
    _genderSwitch.on = _isMale;
}

- (void)switchChanged:(SevenSwitch*)sender
{
    _isMale = sender.on;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if(!( point.x > 0 && point.x < self.frame.size.width && point.y > 0 && point.y < self.frame.size.height ))
    {
        if ([self.delegate respondsToSelector:@selector(OwnerInfoCalloutViewDidPressedOutSide:)])
        {
            [self.delegate OwnerInfoCalloutViewDidPressedOutSide:self];
        }
        [self close];
    }
    
    return [super hitTest:point withEvent:event];
}

#pragma mark - Picker delegate
- (void)datePicker:(FlatDatePicker *)picker didSelectDate:(NSDate *)selectedDate
{
    _birthday = selectedDate;
    _isAmLich = picker.isAmLich;
//    NSLog(@"birthday is %@ %@",[selectedDate stringddmmyyyy],picker.isAmLich?@"AL":@"duong lich");
}

@end
